package org.geometerplus.fbreader.fbreader;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.text.TextUtils;

import com.chineseall.reader.ui.util.GlobalApp;
import com.chineseall.reader.ui.util.ReadSettingUtil;
import com.chineseall.reader.ui.view.readmenu.ReadStyle;
import com.chineseall.reader.ui.view.readmenu.ReadStyleManager;
import com.chineseall.singlebook.R;
import com.iwanvi.common.utils.AndroidUtils;

import org.geometerplus.zlibrary.core.fonts.FontEntry;
import org.geometerplus.zlibrary.core.util.ZLColor;
import org.geometerplus.zlibrary.ui.android.view.ZLAndroidPaintContext;

import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Tony on 2018/11/30.
 */

public enum DrawBuy {

    //绘制标题 标题不允许这行 一行展示完
    DRAW_TITLE {
        @Override
        public void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String CurrTopName, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone) {
            context.setFont(myFontEntry, (int) AndroidUtils.dip2px(GlobalApp.getInstance(), ReadSettingUtil.getReadFontSize() + 5), true, false, false, false);
            int x = mFBView.getLeftMargin() + 10;

            String temp = "列";//计算一个字符的宽度 2个字节的宽度

            int oneWidth = context.getStringWidth(temp);

            //计算一行能够放多少字
            int widthSpec = context.getWidth() - x - mFBView.getLeftMargin();

            int oneSize = (widthSpec / oneWidth) + 1;

            oneSize = CurrTopName.length() <= oneSize ? CurrTopName.length() : oneSize;

            CurrTopName = CurrTopName.substring(0, oneSize);

            context.getStringWidth(CurrTopName);


            topY += context.getStringHeight() * 3;

            context.drawString(x, topY, CurrTopName);
        }
    },

    //绘制内容
    DRAW_CONTENT {
        @Override
        synchronized public void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String CurrTopName, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone) {
            try {

                context.setFont(myFontEntry, (int) AndroidUtils.dip2px(GlobalApp.getInstance(), ReadSettingUtil.getReadFontSize()), false, false, false, false);
                topY += context.getStringHeight() * 3;
                topY += AndroidUtils.dip2px(GlobalApp.getInstance(), 81);

                int strHeight = (int) (context.getStringHeight() + context.getDescent());//在加上一个字体之间的间距

                String temp = "列";//计算一个字符的宽度 2个字节的宽度

                int oneWidth = context.getStringWidth(temp);

                int resdWidth = context.getWidth() - mFBView.getLeftMargin() * 2;

                char[] chars = content.toCharArray();
                StringBuffer sb = new StringBuffer();
                //绘制内容
                int x = mFBView.getLeftMargin();
                int tempX = x;
                for (char c : chars) {

                    if (c == '\r') {
                        sb = new StringBuffer();
                        tempX = x;
                        topY += strHeight;
                    }

                    context.drawString(tempX, topY, c + "");
                    tempX += oneWidth;
                    sb.append(c);
                    if (tempX > resdWidth) {
                        sb = new StringBuffer();
                        topY += context.getStringHeight() + context.getDescent() * 2 + AndroidUtils.dip2px(GlobalApp.getInstance(), ReadSettingUtil.getReadLinespace());
                        tempX = x;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    },

    //绘制阴影
    DRA_SHADOW {
        @Override
        public void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String CurrTopName, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone) {
            //绘制阴影
            int strHeight = (int) (context.getStringHeight() + context.getDescent() + AndroidUtils.dip2px(GlobalApp.getInstance(), ReadSettingUtil.getReadLinespace()));//在加上一个字体之间的间距

            topY = getNewTopY(context);
            int currentBg = ReadStyleManager.getFirstResourceId(ReadSettingUtil.getReadStyle(), ReadStyleManager.OBJ_TYPE.READ);
            int transparent = GlobalApp.getInstance().getResources().getColor(currentBg) & 0xffffff;
            int shadowBg = GlobalApp.getInstance().getResources().getColor(currentBg);
            int ShadowColors[] = new int[]{transparent, shadowBg, shadowBg, shadowBg, shadowBg};
            context.paintStyle(0, (int) (topY - strHeight - AndroidUtils.dip2px(GlobalApp.getInstance(), 60)), context.getWidth(), (int) (topY + context.getHeight()), ShadowColors);//绘制阴影

        }
    },

    //绘制价格
    DRAW_PRICE {
        @Override
        public void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String hint, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone) {
            topY = getNewTopY(context);

            context.setFont(myFontEntry, (int) AndroidUtils.dip2px(GlobalApp.getInstance(), 10), false, false, false, false);

            int hintX = (context.getWidth() - context.getStringWidth(hint)) / 2;

            int hintY = topY - context.getStringHeight() - (int) AndroidUtils.dip2px(GlobalApp.getInstance(), 20);


            context.drawString(hintX, hintY, hint);


            context.setFont(myFontEntry, (int) AndroidUtils.dip2px(GlobalApp.getInstance(), 15), false, false, false, false);
            String[] contents = content.split("\\|");
            String[] startContents = contents[0].split(" ");


            int x = (context.getWidth() - context.getStringWidth(contents[0] + contents[1])) / 2;

            String[] endContents = contents[1].split(" ");
            Pattern p = Pattern.compile("[0-9]*");
            /**
             * 绘制前半部分
             */
            x = drawPrices(startContents, x, topY, context, p);

            /**
             * 绘制竖线
             */
            x = drawVerticalLine(x, topY, context);

            /**
             * 绘制后半部分
             */
            drawPrices(endContents, x, topY, context, p);


        }

        private int drawVerticalLine(int x, int topY, ZLAndroidPaintContext context) {
            context.setLineColor(new ZLColor(getCurrentColor()));
            context.drawString(x, topY, "  ");//绘制空格
            x += context.getStringWidth("  ");
            int lineColor = getLineColor();
            context.setLineColor(new ZLColor(lineColor));
            context.setLineWidth((int) AndroidUtils.dip2px(GlobalApp.getInstance(), 1));
            context.drawLine(x, topY - context.getStringHeight() + 18, x, topY - 5);

            x += context.getStringWidth("  ");
            context.drawString(x, topY, "  ");//绘制空格
            context.setLineWidth((int) AndroidUtils.dip2px(GlobalApp.getInstance(), 1));
            return x;

        }

        private int drawPrices(String[] contents, int x, int topY, ZLAndroidPaintContext context, Pattern p) {

            for (String s : contents) {
                if (!TextUtils.isEmpty(s)) {
                    if (p.matcher(s).matches()) {//绘制数字
                        context.setTextColor(new ZLColor(getCurrentColor()));
                        context.drawString(x, topY, s + " ");
                        x += context.getStringWidth(s + " ");
                    } else {//绘制非数字
                        context.setTextColor(new ZLColor(getProColor()));
                        context.drawString(x, topY, s);
                        x += context.getStringWidth(s);
                    }
                } else {
                    context.drawString(x, topY, " ");//绘制空格
                    x += context.getStringWidth(" ");
                }
            }
            return x;
        }


    },

    //绘制购买本章按钮以及背景
    DRAW_BUY {
        @Override
        public void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String CurrTopName, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone) {

            topY = getNewTopY(context);

            topY += AndroidUtils.dip2px(GlobalApp.getInstance(), 54);
            //绘制购买本章
            int x = (context.getWidth() - context.getStringWidth(content)) / 2;

            context.setFont(myFontEntry, (int) AndroidUtils.dip2px(GlobalApp.getInstance(), 16), true, false, false, false);
            ZLColor mZLColor = new ZLColor(getCurrentColor());
            context.setTextColor(mZLColor);//设置字体颜色
            context.setLineColor(mZLColor);//设置线条颜色
            context.drawString(x, topY, content); //绘制购买

            context.setFont(myFontEntry, (int) AndroidUtils.dip2px(GlobalApp.getInstance(), 15), false, false, false, false);
            context.setLineWidth(AndroidUtils.dip2px(GlobalApp.getInstance(), 0.5f));
            //绘制购买本章的外边框
            context.drawRoundRectangle((int) (AndroidUtils.dip2px(GlobalApp.getInstance(), 16)), (int) (topY - AndroidUtils.dip2px(GlobalApp.getInstance(), 27f)),
                    (int) (context.getWidth() - AndroidUtils.dip2px(GlobalApp.getInstance(), 16)), (int) (topY + AndroidUtils.dip2px(GlobalApp.getInstance(), 14f)), AndroidUtils.dip2px(GlobalApp.getInstance(), 20f));

            /**
             * 添加点击区域
             */
            mRectangleZone.setRect((int) (AndroidUtils.dip2px(GlobalApp.getInstance(), 16)), (int) (topY - AndroidUtils.dip2px(GlobalApp.getInstance(), 27f)),
                    (int) (context.getWidth() - AndroidUtils.dip2px(GlobalApp.getInstance(), 16)), (int) (AndroidUtils.dip2px(GlobalApp.getInstance(), 50)));


        }
    },

    //绘制批量购买优惠
    DRAW_BATCH_BUY {
        @Override
        public void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String CurrTopName, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone) {
            topY = getNewTopY(context);
            topY += AndroidUtils.dip2px(GlobalApp.getInstance(), 54);

            topY += AndroidUtils.dip2px(GlobalApp.getInstance(), 38);

            context.setFont(myFontEntry, (int) AndroidUtils.dip2px(GlobalApp.getInstance(), 12), false, false, false, false);

            //绘制优惠政策
            int x = (context.getWidth() - context.getStringWidth(content)) / 2;


            context.drawString(x, topY, content);

            /**
             * 添加点击区域
             */
            mRectangleZone.setRect(x, topY - context.getStringHeight(), context.getStringWidth(content), context.getStringHeight() + 30);

            int strWidth = context.getStringWidth(content);

            //绘制优惠箭头
            int pading = (int) AndroidUtils.dip2px(GlobalApp.getInstance(), 5);
            context.setLineWidth((int) AndroidUtils.dip2px(GlobalApp.getInstance(), 1));

            context.drawLine(x + strWidth + pading, topY - context.getStringHeight() + 10, x + strWidth + pading + pading, topY - context.getStringHeight() / 2 + 5);

            context.drawLine(x + strWidth + pading + pading, topY - context.getStringHeight() / 2 + 5, x + strWidth + pading, topY);
        }


    },

    //绘制底部线条
    DRAW_LINE {
        @Override
        public void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String CurrTopName, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone) {
            int lineTopY = (int) ((context.getHeight() - AndroidUtils.dip2px(GlobalApp.getInstance(), 50)));
            context.setLineColor(new ZLColor(getLineColor()));
            context.setLineWidth(1f);
            context.drawLine(0, lineTopY, context.getWidth(), lineTopY);//绘制线条
        }
    },

    DRAW_OPEN_VIP {
        @Override
        public void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String CurrTopName, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone) {
            //绘制开通VIP 全站免费

            ZLColor mZLColor = new ZLColor(getCUrrentOpenVipColor());
            context.setTextColor(mZLColor);//设置字体颜色
            context.setLineColor(mZLColor);
            int strWidth = context.getStringWidth(content);
            int lineTopY = (int) ((context.getHeight() - AndroidUtils.dip2px(GlobalApp.getInstance(), 50)));
            lineTopY += AndroidUtils.dip2px(GlobalApp.getInstance(), 30);
            int x = (context.getWidth() - strWidth) / 2;

//            mRectangleZone.setRect(x, (int) (lineTopY - context.getStringHeight()), context.getStringWidth(content) + 30, context.getStringHeight() + 30);
            mRectangleZone.setRect(0, (int) ((context.getHeight() - AndroidUtils.dip2px(GlobalApp.getInstance(), 50))), context.getWidth(), context.getHeight());

            x = onDrawBitmap(context, x, lineTopY);

            context.drawString(x, lineTopY, content);

            //绘制开通VIP箭头
            int pading = (int) AndroidUtils.dip2px(GlobalApp.getInstance(), 5);
            context.setLineWidth((int) AndroidUtils.dip2px(GlobalApp.getInstance(), 1));

            context.drawLine(x + strWidth + pading, lineTopY - context.getStringHeight() + 10, x + strWidth + pading + pading, lineTopY - context.getStringHeight() / 2 + 5);

            context.drawLine(x + strWidth + pading + pading, lineTopY - context.getStringHeight() / 2 + 5, x + strWidth + pading, lineTopY);

        }


        /**
         * 绘制开通VIP图标
         * @param context
         * @param x
         * @param y
         * @return
         */
        public int onDrawBitmap(ZLAndroidPaintContext context, int x, int y) {
            Bitmap bitmap = getCurrentLogo();
            int xX = x + bitmap.getWidth();
            Rect mDesRect = new Rect((int) x, (int) y - bitmap.getHeight(), xX, (int) (y));
            context.drawBitmap(mDesRect, bitmap);
            bitmap.recycle();
            bitmap = null;
            return (int) (xX + AndroidUtils.dip2px(GlobalApp.getInstance(), 5));
        }
    },//绘制开通VIP

    NO_NET_WORK_REFRESH {
        @Override
        public void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String CurrTopName, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone) {

            topY = onDrawBitmap(context, 0, 0);

            context.setFont(myFontEntry, (int) AndroidUtils.dip2px(GlobalApp.getInstance(), 14), false, false, false, false);

            //绘制文字
            int x = (context.getWidth() - context.getStringWidth(content)) / 2;


            topY += AndroidUtils.dip2px(GlobalApp.getInstance(), 50) + context.getStringHeight();

            ZLColor mZLColor = new ZLColor(getCurrentNoWorkColor());
            context.setTextColor(mZLColor);//设置字体颜色

            context.drawString(x, topY, content);


            //绘制重试
            mZLColor = new ZLColor(getCurrentNoWorkRefreshColor());
            context.setTextColor(mZLColor);//设置字体颜色
            context.setLineColor(mZLColor);
            topY += AndroidUtils.dip2px(GlobalApp.getInstance(), 50);
            String refresh = "重试";

            x = (context.getWidth() - context.getStringWidth(refresh)) / 2;

            context.drawString(x, topY, refresh);
            context.setLineWidth(AndroidUtils.dip2px(GlobalApp.getInstance(), 1));
            //绘制购买本章的外边框
            x = (int) ((context.getWidth() - AndroidUtils.dip2px(GlobalApp.getInstance(), 100)) / 2);
            context.drawRoundRectangle(x, (int) (topY - AndroidUtils.dip2px(GlobalApp.getInstance(), 22f)),
                    (int) (x + AndroidUtils.dip2px(GlobalApp.getInstance(), 100)), (int) (topY + AndroidUtils.dip2px(GlobalApp.getInstance(), 12f)), AndroidUtils.dip2px(GlobalApp.getInstance(), 20f));

            /**
             * 添加点击区域
             */
            if (mRectangleZone == null) {
                return;
            }
            mRectangleZone.setRect(x, (int) (topY - AndroidUtils.dip2px(GlobalApp.getInstance(), 27f)),
                    (int) (x + AndroidUtils.dip2px(GlobalApp.getInstance(), 100)), (int) (AndroidUtils.dip2px(GlobalApp.getInstance(), 50)));


        }

        /**
         * 绘制网络异常图片
         * @param context
         * @param x
         * @param y
         * @return
         */
        private int onDrawBitmap(ZLAndroidPaintContext context, int x, int y) {
            Bitmap bitmap = getNotWorkLogo();
            y = (context.getHeight()) / 2;
            int xX = (context.getWidth() - bitmap.getWidth() - x) / 2;
            Rect mDesRect = new Rect(xX, (int) y - bitmap.getHeight(), xX + bitmap.getWidth(), (int) (y));

            context.drawBitmap(mDesRect, bitmap);
            bitmap.recycle();
            return y;

        }
    },

    NOT_NET_WORK_SETTING {
        @Override
        public void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String CurrTopName, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone) {

        }


    };//无网络的情况

    protected int getNewTopY(ZLAndroidPaintContext context) {
        return (int) (context.getHeight() - AndroidUtils.dip2px(GlobalApp.getInstance(), 246) + AndroidUtils.dip2px(GlobalApp.getInstance(), 41));

    }

    /**
     * 获取当前颜色
     *
     * @return
     */
    protected @ColorInt
    int getCurrentColor() {
        ReadStyle style = ReadSettingUtil.getReadStyle();
        return ReadStyleManager.getResourceIdByStyle(textColors, style);
    }

    protected @ColorInt
    int getCurrentNoWorkColor() {
        ReadStyle style = ReadSettingUtil.getReadStyle();
        return ReadStyleManager.getResourceIdByStyle(noWorkTextColors, style);
    }

    protected @ColorInt
    int getCUrrentOpenVipColor() {
        ReadStyle style = ReadSettingUtil.getReadStyle();
        return ReadStyleManager.getResourceIdByStyle(openVipColors, style);
    }

    protected @ColorInt
    int getCurrentNoWorkRefreshColor() {

        ReadStyle style = ReadSettingUtil.getReadStyle();
        return ReadStyleManager.getResourceIdByStyle(noWorkTextRefreshColors, style);
    }

    /**
     * 重置字体颜色
     *
     * @return
     */
    protected @ColorInt
    int restoreColor() {
        return 0xFF133755;
    }

    protected Bitmap getNotWorkLogo() {
        ReadStyle style = ReadSettingUtil.getReadStyle();
        int resId = R.drawable.common_state_no_read_net;
        try {
            resId = ReadStyleManager.getResourceIdByStyle(unWorkLogos, style);

        } catch (Exception e) {

        }
        return BitmapFactory.decodeResource(GlobalApp.getInstance().getResources(), resId);
    }

    protected Bitmap getCurrentLogo() {
        ReadStyle style = ReadSettingUtil.getReadStyle();
        if (style == ReadStyle.NIGHT) {
            return BitmapFactory.decodeResource(GlobalApp.getInstance().getResources(), vipLogos[0]);
        } else {
            return BitmapFactory.decodeResource(GlobalApp.getInstance().getResources(), vipLogos[1]);
        }
    }

    protected @ColorInt
    int getLineColor() {
        ReadStyle style = ReadSettingUtil.getReadStyle();
        return ReadStyleManager.getResourceIdByStyle(hLine, style);
    }

    protected @ColorInt
    int getProColor() {

        ReadStyle style = ReadSettingUtil.getReadStyle();
        return ReadStyleManager.getResourceIdByStyle(proTextColor, style);
    }

    //index顺序 夜晚、白天(默认)、桃花、舒适、宁静、怀古、护眼
    private int[] unWorkLogos = {R.drawable.un_night_state_no_read_net, R.drawable.common_state_no_read_net,
            R.drawable.un_peachblossom_state_no_read_net, R.drawable.un_comfort_state_no_read_net,
            R.drawable.un_quiet_state_no_read_net, R.drawable.un_nostalgia_state_no_read_net, R.drawable.common_state_no_read_net};

    private int[] vipLogos = {R.drawable.icon_vip_logo_black, R.drawable.icon_vip_logo_light};

    private int[] textColors = {0xFF004A8D, 0xFF2776FE, 0xFFB31717, 0xFF2F9570, 0xFF0D65A8, 0xFFAF5700, 0xFF2F9570};

    private int[] openVipColors = {0xFF004A8D, 0xFFFFAA56, 0xFFB31717, 0xFF2F9570, 0xFF0D65A8, 0xFFAF5700, 0xFF2F9570};

    private int[] noWorkTextColors = {0xFF004A8D, 0xFF999999, 0xFFAA5E5E, 0xFF33715A, 0xFF2D597B, 0xFF9B5E21, 0xFFAA5E5E};

    private int[] noWorkTextRefreshColors = {0xFF004A8D, 0xFF2776FE, 0xFFA75858, 0xFF33715A, 0xFF2D597B, 0xFF9B5E21, 0xFFAA5E5E};

    private int[] hLine = {0xFF333333, 0xFFEEEEEE, 0xFFB77373, 0xFFA4C3A6, 0xFF7493C1, 0xFFB77373, 0xFFAF5700, 0xFF2F9570, 0xFF2F9570};

    private int[] proTextColor = {0xFF555555, 0xFF333333, 0xFF4A221D, 0xFF184F3B, 0xFF133755, 0xFF4A321D, 0xFF4A321D};

    public abstract void draw(FBView mFBView, ZLAndroidPaintContext context, int topY, String CurrTopName, String content, List<FontEntry> myFontEntry, TapZoneMap.RectangleZone mRectangleZone);


}
